package com.apobates.jforum2.troll.replies.biz.impl.api;

import com.apobates.jforum2.troll.regular.ActionOperatorLog;
import com.apobates.jforum2.troll.regular.ActionOperatorLogProvider;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.regular.event.PostsActionLog;
import com.apobates.jforum2.troll.replies.entity.Posts;
import com.apobates.jforum2.troll.replies.mapper.ReplyVoMapperEntity;
import com.apobates.jforum2.troll.utils.core.api.ActionEventCulpritor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * SpringCloudStream RabbitMQ操作日志处理接口
 * 谁负责谁声明.若将StreamBridge提到regular模块中该如何声明呢？
 */
@Component
public class PostsActionLogStreamProvider implements ActionOperatorLogProvider {
    @Autowired
    private StreamBridge streamBridge;
    private final static Logger logger = LoggerFactory.getLogger(PostsActionLogStreamProvider.class);

    @Override
    public void send(ActionOperatorLog log) {
        logger.error("[LOG][Replies]start send");
        streamBridge.send(log.getBindingNames()+"-out-0", log.getPayload());
        logger.error("[LOG][Replies]send finish");
    }

    public void action(final ForumActionEnum action, Posts posts, final ActionEventCulpritor culpritor){
        PostsActionLog pa = new PostsActionLog();
        pa.setMemberId(culpritor.getMemberId());
        pa.setMemberNickname(culpritor.getMemberNickname());
        pa.setAction(action);
        pa.setTopicId(posts.getTopicId());
        //pa.setTopicTitle();
        pa.setPostsId(posts.getId());
        pa.setRodeMember(culpritor.getMemberId());
        pa.setIpAddr(culpritor.getIpAddr());
        pa.setToken(culpritor.getToken());
        pa.setDateTime(LocalDateTime.now());
        String postsLink = ReplyVoMapperEntity.generateLink(posts);
        pa.setLink(postsLink);
        this.send(pa);
    }

    //public void entryTopicContent(long topicId, long topicContentId, ActionEventCulpritor culpritor){
    public void entryTopicContent(Posts posts, ActionEventCulpritor culpritor){
        PostsActionLog pa = new PostsActionLog();
        pa.setMemberId(culpritor.getMemberId());
        pa.setMemberNickname(culpritor.getMemberNickname());
        pa.setAction(ForumActionEnum.POSTS_REPLY);
        pa.setTopicId(posts.getTopicId());
        //pa.setTopicTitle();
        pa.setPostsId(posts.getId());
        pa.setRodeMember(culpritor.getMemberId());
        pa.setIpAddr(culpritor.getIpAddr());
        pa.setToken(culpritor.getToken());
        pa.setDateTime(LocalDateTime.now());
        String postsLink = ReplyVoMapperEntity.generateLink(posts);
        pa.setLink(postsLink);
        this.send(pa);
    }
}
